Add a color picker implementation for gnome-shell
authorMatthias Clasen <mclasen@redhat.com>
Thu, 26 Jul 2018 22:50:03 +0000 (18:50 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Mon, 30 Jul 2018 22:10:17 +0000 (18:10 -0400)
This adds a GtkColorPicker implementation that talks
to gnome-shell to get a color.

gtk/Makefile.am
gtk/gtkcolorpicker.c
gtk/gtkcolorpickershell.c [new file with mode: 0644]
gtk/gtkcolorpickershellprivate.h [new file with mode: 0644]

index 94ef6d0c27c48fed2fe458beac3faaa0ec73bf14..2b6b55f4af380fd5e32a0cb246ee56bfcb54dd8a 100644 (file)
@@ -400,6 +400,7 @@ gtk_private_h_sources =             \
        gtkcoloreditorprivate.h \
        gtkcolorpickerprivate.h \
        gtkcolorpickerportalprivate.h   \
+       gtkcolorpickershellprivate.h    \
        gtkcolorplaneprivate.h  \
        gtkcolorscaleprivate.h  \
        gtkcolorchooserprivate.h        \
@@ -679,6 +680,7 @@ gtk_base_c_sources =                \
        gtkcoloreditor.c        \
        gtkcolorpicker.c        \
        gtkcolorpickerportal.c  \
+       gtkcolorpickershell.c   \
        gtkcolorplane.c         \
        gtkcolorscale.c         \
        gtkcolorswatch.c        \
index d270a06be2fd63fee17ec4d6b056f8ce859007aa..b3ac5dce78fecede4cd5ec59578eecaa8bb29630 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "gtkcolorpickerprivate.h"
 #include "gtkcolorpickerportalprivate.h"
+#include "gtkcolorpickershellprivate.h"
 #include "gtkprivate.h"
 #include <gio/gio.h>
 
@@ -53,6 +54,6 @@ gtk_color_picker_new (void)
   if (gtk_should_use_portal ())
     return gtk_color_picker_portal_new ();
   else
-    return NULL;
+    return gtk_color_picker_shell_new ();
 }
 
diff --git a/gtk/gtkcolorpickershell.c b/gtk/gtkcolorpickershell.c
new file mode 100644 (file)
index 0000000..d465e0a
--- /dev/null
@@ -0,0 +1,183 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 2018, Red Hat, Inc
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "gtkcolorpickershellprivate.h"
+#include <gio/gio.h>
+
+struct _GtkColorPickerShell
+{
+  GObject parent_instance;
+
+  GDBusProxy *shell_proxy;
+  GTask *task;
+};
+
+struct _GtkColorPickerShellClass
+{
+  GObjectClass parent_class;
+};
+
+static GInitableIface *initable_parent_iface;
+static void gtk_color_picker_shell_initable_iface_init (GInitableIface *iface);
+static void gtk_color_picker_shell_iface_init (GtkColorPickerInterface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (GtkColorPickerShell, gtk_color_picker_shell, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, gtk_color_picker_shell_initable_iface_init)
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_COLOR_PICKER, gtk_color_picker_shell_iface_init))
+
+static gboolean
+gtk_color_picker_shell_initable_init (GInitable     *initable,
+                                      GCancellable  *cancellable,
+                                      GError       **error)
+{
+  GtkColorPickerShell *picker = GTK_COLOR_PICKER_SHELL (initable);
+  char *owner;
+
+  picker->shell_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                                        G_DBUS_PROXY_FLAGS_NONE,
+                                                        NULL,
+                                                        "org.gnome.Shell.Screenshot",
+                                                        "/org/gnome/Shell/Screenshot",
+                                                        "org.gnome.Shell.Screenshot",
+                                                        NULL,
+                                                        error);
+
+  if (picker->shell_proxy == NULL)
+    {
+      g_debug ("Failed to create shell screenshot proxy");
+      return FALSE;
+    }
+
+  owner = g_dbus_proxy_get_name_owner (picker->shell_proxy);
+  if (owner == NULL)
+    {
+      g_debug ("org.gnome.Shell.Screenshot not provided");
+      g_clear_object (&picker->shell_proxy);
+      return FALSE;
+    }
+  g_free (owner);
+
+  return TRUE;
+}
+
+static void
+gtk_color_picker_shell_initable_iface_init (GInitableIface *iface)
+{
+  initable_parent_iface = g_type_interface_peek_parent (iface);
+  iface->init = gtk_color_picker_shell_initable_init;
+}
+
+static void
+gtk_color_picker_shell_init (GtkColorPickerShell *picker)
+{
+}
+
+static void
+gtk_color_picker_shell_finalize (GObject *object)
+{
+  GtkColorPickerShell *picker = GTK_COLOR_PICKER_SHELL (object);
+
+  g_clear_object (&picker->shell_proxy);
+
+  G_OBJECT_CLASS (gtk_color_picker_shell_parent_class)->finalize (object);
+}
+
+static void
+gtk_color_picker_shell_class_init (GtkColorPickerShellClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  object_class->finalize = gtk_color_picker_shell_finalize;
+}
+
+GtkColorPicker *
+gtk_color_picker_shell_new (void)
+{
+  return GTK_COLOR_PICKER (g_initable_new (GTK_TYPE_COLOR_PICKER_SHELL, NULL, NULL, NULL));
+}
+
+static void
+color_picked (GObject      *source,
+              GAsyncResult *res,
+              gpointer      data)
+{
+  GtkColorPickerShell *picker = GTK_COLOR_PICKER_SHELL (data);
+  GError *error = NULL;
+  GVariant *ret;
+
+  ret = g_dbus_proxy_call_finish (picker->shell_proxy, res, &error);
+
+  if (ret == NULL)
+    {
+      g_task_return_error (picker->task, error);
+    }
+  else
+    {
+      GdkRGBA c;
+
+      c.alpha = 1;
+      if (!g_variant_lookup (ret, "color", "(ddd)", &c.red, &c.green, &c.blue))
+        g_task_return_new_error (picker->task, G_IO_ERROR, G_IO_ERROR_FAILED, "No color received");
+      else
+        g_task_return_pointer (picker->task, gdk_rgba_copy (&c), (GDestroyNotify)gdk_rgba_free);
+
+      g_variant_unref (ret);
+    }
+
+  g_clear_object (&picker->task);
+}
+
+static void
+gtk_color_picker_shell_pick (GtkColorPicker      *cp,
+                             GAsyncReadyCallback  callback,
+                             gpointer             user_data)
+{
+  GtkColorPickerShell *picker = GTK_COLOR_PICKER_SHELL (cp);
+
+  if (picker->task)
+    return;
+
+  picker->task = g_task_new (picker, NULL, callback, user_data);
+
+  g_dbus_proxy_call (picker->shell_proxy,
+                     "PickColor",
+                     NULL,
+                     0,
+                     -1,
+                     NULL,
+                     color_picked,
+                     picker);
+}
+
+static GdkRGBA *
+gtk_color_picker_shell_pick_finish (GtkColorPicker  *cp,
+                                     GAsyncResult    *res,
+                                     GError         **error)
+{
+  g_return_val_if_fail (g_task_is_valid (res, cp), NULL);
+
+  return g_task_propagate_pointer (G_TASK (res), error);
+}
+
+static void
+gtk_color_picker_shell_iface_init (GtkColorPickerInterface *iface)
+{
+  iface->pick = gtk_color_picker_shell_pick;
+  iface->pick_finish = gtk_color_picker_shell_pick_finish;
+}
diff --git a/gtk/gtkcolorpickershellprivate.h b/gtk/gtkcolorpickershellprivate.h
new file mode 100644 (file)
index 0000000..c41b086
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * GTK - The GIMP Toolkit
+ * Copyright (C) 2018 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This Library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This Library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GTK_COLOR_PICKER_SHELL_H__
+#define __GTK_COLOR_PICKER_SHELL_H__
+
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtkcolorpickerprivate.h>
+
+G_BEGIN_DECLS
+
+
+#define GTK_TYPE_COLOR_PICKER_SHELL gtk_color_picker_shell_get_type ()
+G_DECLARE_FINAL_TYPE (GtkColorPickerShell, gtk_color_picker_shell, GTK, COLOR_PICKER_SHELL, GObject)
+
+GDK_AVAILABLE_IN_ALL
+GtkColorPicker * gtk_color_picker_shell_new (void);
+
+G_END_DECLS
+
+#endif  /* __GTK_COLOR_PICKER_SHELL_H__ */